#region The MIT License
// The MIT License
// 
// Copyright (c) 2011 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System.Collections.Generic;
using System.Reflection;

namespace octalforty.Staytuned.ObjectModel
{
    /// <summary>
    /// Represents an abstract Notification.
    /// </summary>
    public class Notification
    {
        /// <summary>
        /// Gets or sets a <see cref="SenderDescriptor"/> which describes
        /// the sender of this <see cref="Notification"/>.
        /// </summary>
        public SenderDescriptor Sender
        { get; set; }

        /// <summary>
        /// Gets or sets an <see cref="Address"/> which describes and address
        /// used to reply to this <see cref="Notification"/>.
        /// </summary>
        public Address ReplyTo
        { get; set; }

        /// <summary>
        /// Gets a list of <see cref="Attachment"/> objects.
        /// </summary>
        public IList<Attachment> Attachments
        { get; private set; }

        /// <summary>
        /// Gets a dictionary of data items used to render templates.
        /// </summary>
        public IDictionary<string, object> Data
        { get; private set; }

        /// <summary>
        /// Gets a dictionary of additional headers to be passed along with this <see cref="Notification"/>.
        /// </summary>
        public IDictionary<string, string> Headers
        { get; private set;}

        /// <summary>
        /// Initializes a new instance of the <see cref="Notification"/> class.
        /// </summary>
        public Notification()
        {
            Attachments = new List<Attachment>();
            Data = new Dictionary<string, object>();
            Headers = new Dictionary<string, string>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Notification"/> class.
        /// </summary>
        /// <param name="data">An anonymous object used to populate <see cref="Data"/> property.</param>
        /// <param name="sender"></param>
        public Notification(object data, SenderDescriptor sender) :
            this()
        {
            if(data != null)
            {
                foreach(var property in data.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    var name = property.Name;
                    var value = property.GetValue(data, null);

                    Data[name] = value;
                } // foreach
            } // if

            Sender = sender;
        }
    }
}